// pages/ChangePw/changepw.js

const http=require('../../utils/http')

Page({

  /**
   * Page initial data
   */
  data: {
     phone:"",
     code:"",
     newPd:"",
     code_content:"获取验证码",
     duration:60,
     status:0
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {
    let that=this
  },

  bindPhoneInput:function (e) {
    this.setData({
       phone:e.detail.value
    })
  },

  bindVerCodeInput:function (e) {
    this.setData({
       code:e.detail.value
    })
  },

  bindNewPwInput:function (e) {
    this.setData({
       newPd:e.detail.value
    })
  },

   getVerCode(){
     if(this.data.phone.length<11){
       wx.showToast({
         title: '请输入正确手机号',
         icon:'none'
       })
       return
     }
     if(this.data.status==0){
      this.start()

      let params={
        "staff_mobile":this.data.phone
      }
      http.postData("api/sys/send_sms",params)
      .then((data)=>{
         wx.showToast({
           title: data['msg'],
         })
      })
      .catch((reason)=>{
         
      })
     }
  },

   start() {
   var that=this
    var timer = setInterval(function() {
      that.setData({ 
         duration:that.data.duration-1,
         code_content:that.data.duration+'s',
         status:1,
      })
      // 当倒计时为0时，启用按钮并清除计时器
      if (that.data.duration === 0) {
        clearInterval(timer);
        that.setData({
           duration:60,
           code_content:'获取验证码',
           status:0,
        })
      }
    }, 1000);
  },

  submit(){
     if(this.data.phone.length<11){
       wx.showToast({
         title: '请输入手机号',
         icon:'none'
       })
       return
     }
     if(this.data.code.length<6){
      wx.showToast({
        title: '请输入验证码',
        icon:'none'
      })
      return
    }
    if(this.data.newPd.length==0){
      wx.showToast({
        title: '请输入新密码',
        icon:'none'
      });
      return
    }
    wx.showLoading({
      title: '请稍等...',
     })

     var params={
      'staff_mobile':this.data.phone,
      'sms_code':this.data.code,
      'pwd_new':this.data.newPd
     }
     http.postData("api/sys/reset_password",params)
     .then((value)=>{
        wx.hideLoading()
        wx.showToast({
          title: value['msg'],
          icon:'none'
        })
        .then((v)=>{
          if(value['code']==0){
            wx.navigateBack()
         }
        })
       
     })
     .catch((reason)=>{
         wx.hideLoading()
         wx.showToast({
           title: reason['msg'],
           icon:'none'
         })
     })

  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  }
})